//
//  ViewController.m
//  Cell
//
//  Created by WLJ on 19/10/2016.
//  Copyright © 2016 WLJ. All rights reserved.
//

#import "ViewController.h"
#import "MyTableViewCell.h"
#import "Model.h"
#import "ViewModel.h"
#import "OtherViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * data;
@end

@implementation ViewController

-(void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray * tips = @[@"2014毕业设计",@"2015毕业设计",@"",@"2015毕业设计",@"2016毕业设计",@"2016毕业设计",@"2016毕业设计",@"2017毕业设计",@"2017毕业设计",@"2018毕业设计",@"2019毕业设计",@"2019毕业设计",@"2019毕业设计"];

    NSArray * name = @[@"SA",@"掌声",@"撒旦",@"安定",@"个人",@"还挺好",@"阿迪我",@"核弹",@"加一句",@"个个",@"光荣感",@"人工的",@"76方式"];
    NSArray * classs = @[@"营销1302",@"电子102",@"音乐120",@"物理002",@"机械101",@"自动化101",@"土木工程1310",@"纺织230",@"体育140",@"生化129",@"外语112",@"测试132",@"计算机134"];
    NSArray * physical = @[@"营销管理",@"电子信息工程",@"音乐艺术",@"物理",@"机械设计制造与自动化",@"自动化",@"土木工程",@"纺织服装",@"体育",@"生化",@"外语112",@"测试",@"计算机"];
    NSArray * numb = @[@"M122",@"X200",@"3203330230",@"339212303",@"1223455543",@"R2134142X",@"123134341",@"53454354",@"656565656",@"67u67u6776",@"h56564g",@"342E34WQ2",@"D34123444444"];
    NSArray * phone = @[@"13322345678",@"18077367286",@"13905539098",@"13899890097",@"18755657876",@"18355657764",@"13665567875",@"18344557765",@"7287655",@"0553-9877687",@"95877",@"12223331",@"66676554"];
    NSArray * header = @[@"http://g.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=1ec432c69a3df8dca6688795f8215ebd/7dd98d1001e939014f34b10f7eec54e736d196c8.jpg",@"http://d.lanrentuku.com/down/png/1607/bolt-icons/bolt_stay.png",@"",@"http://d.lanrentuku.com/down/png/1607/bolt-icons/mittens.png",@"http://d.lanrentuku.com/down/png/1607/bolt-icons/rhino.png",@"http://d.lanrentuku.com/down/png/1607/bolt-icons/penny.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_002.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_004.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_011.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_003.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_010.png",@"http://d.lanrentuku.com/down/png/1512/2015sdj/2015sdj_008.png",@"",@"",@""];
    for (int i = 0; i<10; i++) {
        Model * model = [[Model alloc]init];
        model.tip = tips[i];
        model.name = name[i];
        model.headerUrl = header[i];
        model.classs = classs[i];
        model.physical = physical[i];
        model.numb = numb[i];
        model.phone = phone[i];
        [self.data addObject:[ViewModel viewModelWithMode:model]];
    }
    [self.tableView reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)bindCell:(id)cell withViewModel:(id)viewModel{
    
    MyTableViewCell * cells = cell;
    ViewModel * viewModels = viewModel;
    @weakify(cells)
    [[RACObserve(viewModels, tip) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.tip.text = x;
    }];
    [[RACObserve(viewModels, headerUrl) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        [cells.header sd_setImageWithURL:[NSURL URLWithString:x] placeholderImage:[UIImage imageNamed:@"组长0"] options:SDWebImageRetryFailed];
    }];
    [[RACObserve(viewModels, name ) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.name.text = x;
    }];
    
    [[RACObserve(viewModels, classs) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.classs.label.text = x;
    }];
    [[RACObserve(viewModels, physical) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.physical.label.text = x;
    }];
    [[RACObserve(viewModels, phone) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.phone.label.text = x;
    }];
    [[RACObserve(viewModels, numb) takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(NSString * x) {
        @strongify(cells)
        cells.numb.label.text = x;
    }];

    @weakify(viewModels)
    [[[cells.btn0 rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cells.rac_prepareForReuseSignal ] subscribeNext:^(id x) {
        @strongify(viewModels)
        [[viewModels.btn0Command execute:viewModels] subscribeNext:^(id x) {
            StandAlert(x)
        } error:^(NSError *error) {
            StandAlert(error.domain)
        }];
    }];
    [[[cells.btn1 rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cells.rac_prepareForReuseSignal ] subscribeNext:^(id x) {
        @strongify(viewModels)
        [[viewModels.btn0Command execute:viewModels] subscribeNext:^(id x) {
            StandAlert(x)
        } error:^(NSError *error) {
            StandAlert(error.domain)
        }];
    }];
    [[[cells.btn2 rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cells.rac_prepareForReuseSignal ] subscribeNext:^(id x) {
        @strongify(viewModels)
        [[viewModels.btn0Command execute:viewModels] subscribeNext:^(id x) {
            StandAlert(x)
        } error:^(NSError *error) {
            StandAlert(error.domain)
        }];
    }];
    [[[cells.btn3 rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cells.rac_prepareForReuseSignal ] subscribeNext:^(id x) {
        @strongify(viewModels)
        [[viewModels.btn0Command execute:viewModels] subscribeNext:^(id x) {
            StandAlert(x)
        } error:^(NSError *error) {
            StandAlert(error.domain)
        }];
    }];
    
    [[[cells.rightTopBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cells.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        
        [self.navigationController pushViewController:({
            OtherViewController * otherVC = [[OtherViewController alloc]init];
            otherVC.navigationItem.title = viewModels.name;
            otherVC;
        }) animated:YES];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return rowHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ViewModel* model = self.data[indexPath.row];
    [self bindCell:cell withViewModel:model];
    return cell;
}



-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        [self.view addSubview:self.tableView];
        _tableView.delegate  =self;
        _tableView.dataSource = self;
        [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    }
    return  _tableView;
}

-(NSMutableArray *)data{
    if (!_data) {
        _data = [NSMutableArray arrayWithCapacity:0];
    }
    return _data;
}
@end
